package rmmk.zadanie1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

public class LatexOutput {
	
	String latexFilename = "latex";
	String plotFilename = "plot.txt";
	
	public void printLatexOutput(String filePrefix, Map<Integer, ArrayList<Long>> observations) {
		try{
		File lateXfile = new File(filePrefix + latexFilename);
		File plotFile = new File(filePrefix + plotFilename);
		
		if (!lateXfile.exists()) {
			lateXfile.createNewFile();
		}
		if (!plotFile.exists()) {
			plotFile.createNewFile();
		}
		
		FileWriter lf = new FileWriter(lateXfile.getAbsoluteFile());
		BufferedWriter late = new BufferedWriter(lf);
		FileWriter fw = new FileWriter(plotFile.getAbsoluteFile());
		BufferedWriter plot = new BufferedWriter(fw);
		
		late.write("Wielość tablicy & średni czas sortowania & najkrótszy czas sortowania & najdłuższy czas sortowania ");
		
		for(Entry<Integer, ArrayList<Long>> e : observations.entrySet()){

			double avgTime = 0.0;
			long minTime = e.getValue().get(0);
			long maxTime = e.getValue().get(0);
			
			for(Long l : e.getValue())
				avgTime+=l;
			
			avgTime/=e.getValue().size();
			
			late.write(e.getKey() + " & " + avgTime + " & " + minTime + " & " + maxTime);
			plot.write(e.getKey() + " " + avgTime);
		}
		
		late.close();
		late.close();
		}catch (IOException exc){
			exc.printStackTrace();
		}
	}
}
